<?php
/**
 * 数据字典
 * User: LUOQIN
 * Date: 2017/7/20 0020
 * Time: 09:48
 */
namespace app\admin\controller;
use think\Db;

class Table extends \think\Controller
{
    /**
     * @var string 数据库名
     */
    private static $databasename = 'shaohuo';
    /**
     * @var string 数据连接
     */
    private static $conn = 'mysql://root:Jiuduo123@122.112.200.214:3306/information_schema#utf8';

    //获取表名等相关内容
    public function index(){
        $f='TABLE_NAME,ENGINE,CREATE_TIME,UPDATE_TIME,TABLE_COLLATION,TABLE_COMMENT';
        $w=['TABLE_SCHEMA'=>self::$databasename];
        $re = Db::connect(self::$conn)->table('TABLES')->field($f)->where($w)->order('CREATE_TIME asc')->select();
        $this->assign('databasename',self::$databasename);
        $this->assign('rows',$re);
        return $this->fetch();
    }

    //获取表字段
    public function showfield(){
        //接受表名
        $val = input('param.tablename');

        $f='co.TABLE_SCHEMA,co.TABLE_NAME,co.COLUMN_NAME,co.COLUMN_DEFAULT,co.DATA_TYPE,co.COLUMN_TYPE,co.CHARACTER_SET_NAME,co.COLLATION_NAME,co.EXTRA,co.COLUMN_COMMENT';
        $f.=',s.INDEX_NAME';
        $w=[
            'co.TABLE_SCHEMA'=>self::$databasename,
            'co.TABLE_NAME'=>$val,
        ];
        $j = [
            ['STATISTICS s','s.COLUMN_NAME=co.COLUMN_NAME and s.TABLE_SCHEMA="'.self::$databasename.'" and s.TABLE_NAME="'.$val.'"','LEFT'],
        ];
        $re = Db::connect(self::$conn)->table('COLUMNS')->alias('co')->field($f)->where($w)->join($j)->order('co.ORDINAL_POSITION asc')->select();

        return json($re);
    }
}